import Router from 'vue-router'
import Home from '@/components/Home.vue'
import Chances from '@/components/Chances.vue'
import ChancesPlan from '@/components/ChancesPlan.vue'
import Customer from '@/components/Customer.vue'
import CustomerInfo from '@/components/CustomerInfo.vue'
import CustomerContact from '@/components/CustomerContact.vue'
import CustomerRelationship from '@/components/CustomeRelationship.vue'
import CustomerOrders from '@/components/CustomeOrders.vue'
import CustomeOrderDetails from '@/components/CustomeOrderDetails.vue'
import CustomerLost from '@/components/CustomerLost.vue'
import ServiceCreate from '@/components/ServiceCreate.vue'
import ServiceAllot from '@/components/ServiceAllot.vue'
import ServiceHandle from '@/components/ServiceHandle.vue'
import ServiceFeedback from '@/components/ServiceFeedback.vue'
import ServiceArchive from '@/components/ServiceArchive.vue'
import ContributionAnalysis from '@/components/ContributionAnalysis.vue'
import ComponentAnalysis from '@/components/ComponentAnalysis.vue'
import ServeAnalysis from '@/components/ServeAnalysis.vue'
import LostAnalysis from '@/components/LostAnalysis'
import ChancesApp from '@/components/ChancesApp'
import AddChances from '@/components/AddChances'
import Login from '@/components/Login'
import index from '@/components/index.vue'
import UserGrade from '@/components/UserGrade.vue'
import Serve from '@/components/Serve.vue'
import Area from '@/components/Area.vue'
import Product from '@/components/Product.vue'
import Stock from '@/components/Stock.vue'
import PermissionAndUsers from '@/components/PermissionAndUsers'
import PermissionError from '@/components/PermissionError.vue'

//配置路由数组
const routes = [{
	path: '/',
	component: Login,
	meta: {
		authorize: [1, 2, 3, 4, 5]
	}
}, {

	name: 'L0500',
	path: '/index',
	component: index,
	meta: {
		authorize: [1, 2, 3, 4, 5]
	},
	children: [{
		name: 'home',
		path: '/home',
		meta: {
			title: '首页',
			authorize: [1, 2, 3, 4, 5]
		},
		component: Home
	}, {
		name: 'L0101',
		path: '/marketing/chances',
		meta: {
			title: '销售机会管理',
			authorize: [2, 3, 5]
		},
		component: Chances
	}, {
		name: 'L0102',
		path: '/marketing/chancesPlan',
		meta: {
			title: '客户开发计划',
			authorize: [2, 3, 5]
		},
		component: ChancesPlan
	}, {
		name: 'L0201',
		path: '/customer/customerInfo',
		meta: {
			title: '客户管理',
			authorize: [3, 5]
		},
		component: Customer,
	}, {
		name: 'editInfo',
		path: '/customer/customerInfo/editInfo',
		meta: {
			title: '客户信息',
			authorize: [3, 5]
		},
		component: CustomerInfo,
	}, {
		name: 'contact',
		path: '/customer/customerInfo/editInfo/contact',
		meta: {
			title: '联系人',
			authorize: [3, 5]
		},
		component: CustomerContact,
	}, {
		name: 'relationship',
		path: '/customer/customerInfo/relationship',
		meta: {
			title: '交往记录',
			authorize: [3, 5]
		},
		component: CustomerRelationship,
	}, {
		name: 'orders',
		path: '/customer/customerInfo/orders',
		meta: {
			title: '历史订单',
			authorize: [3, 5]
		},
		component: CustomerOrders,
	},
	{
		name: 'orderDetails',
		path: '/customer/customerInfo/orders/orderdetails',
		meta: {
			title: '订单明细',
			authorize: [3, 5]
		},
		component: CustomeOrderDetails,
	}, {
		name: 'L0202',
		path: '/customer/customerLost',
		meta: {
			title: '客户流失管理',
			authorize: [3, 5]
		},
		component: CustomerLost
	}, {
		name: 'L0301',
		path: '/service/serviceCreate',
		meta: {
			title: '服务创建',
			authorize: [3, 5]
		},
		component: ServiceCreate
	}, {
		name: 'L0302',
		path: '/service/serviceAllot',
		meta: {
			title: '服务分配',
			authorize: [2, 5]
		},
		component: ServiceAllot
	}, {
		name: 'L0303',
		path: '/service/serviceHandle',
		meta: {
			title: '服务处理',
			authorize: [3, 5]
		},
		component: ServiceHandle
	}, {
		name: 'L0304',
		path: '/service/serviceFeedback',
		meta: {
			title: '服务反馈',
			authorize: [3, 5]
		},
		component: ServiceFeedback
	}, {
		name: 'L0305',
		path: '/service/serviceArchive',
		meta: {
			title: '服务归档',
			authorize: [2, 3, 5]
		},
		component: ServiceArchive
	}, {
		name: 'L0401',
		path: '/report/contributionAnalysis',
		meta: {
			title: '客户贡献分析',
			authorize: [2, 4, 5]
		},
		component: ContributionAnalysis
	}, {
		name: 'L0402',
		path: '/report/componentAnalysis',
		meta: {
			title: '客户构成分析',
			authorize: [2, 4, 5]
		},
		component: ComponentAnalysis
	}, {
		name: 'L0403',
		path: '/report/serveAnalysis',
		meta: {
			title: '客户服务分析',
			authorize: [2, 4, 5]
		},
		component: ServeAnalysis
	}, {
		name: 'L0404',
		path: '/report/lostAnalysis',
		meta: {
			title: '客户流失分析',
			authorize: [2, 4, 5]
		},
		component: LostAnalysis
	},
	{
		name: 'L0405',
		path: '/marketing/chancesPlan/ChancesApp',
		component: ChancesApp,
		meta: {
			title: '制定计划',
			authorize: [2, 3, 5]
		}
	}, {
		name: 'L0406',
		path: '/marketing/Addchances',
		component: AddChances,
		meta: {
			title: '执行计划',
			authorize: [3, 5]
		}
	}, {
		name: 'L0501',
		path: '/basis/userGrade',
		component: UserGrade,
		meta: {
			title: '客户等级',
			authorize: [1, 5]
		}
	}, {
		name: 'L0502',
		path: '/basisrve',
		component: Serve,
		meta: {
			title: '服务类型',
			authorize: [1, 5]
		}
	}, {
		name: 'L0503',
		path: '/basis/area',
		component: Area,
		meta: {
			title: '地区类型',
			authorize: [1, 5]
		}
	}, {
		name: 'L0504',
		path: '/basis/product',
		component: Product,
		meta: {
			title: '产品信息',
			authorize: [3, 5]
		}
	}, {
		name: 'L0505',
		path: '/basis/stock',
		component: Stock,
		meta: {
			title: '库存',
			authorize: [3, 5]
		}
	}, {
		name: 'L0601',
		path: '/permission/Users',
		meta: {
			title: '用户管理',
			authorize: [1, 5]
		},
		component: PermissionAndUsers
	}, {
		path: '/permissionError',
		component: PermissionError,
		meta: {
			title: '权限不足',
			authorize: [1, 2, 3, 4, 5]
		},
	}]
}]
const router = new Router({
	routes
});

//路由拦截
router.beforeEach((to, from, next) => {
	let role = parseInt(localStorage.getItem("nId")) || '';
	if (role != null && role != '' && to.meta.authorize !== undefined) {
		if (to.meta.authorize.indexOf(role) !== -1) {
			next();
		} else {
			next('/permissionError');
		}
	} else if (to.meta.authorize !== undefined && to.path !== '/') {
		next('/');
	} else {
		next();
	}
});

export default router;
